package com.redhat.auth.ftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

import com.redhat.auth.ftp.exception.FtpException;
import com.redhat.auth.ftp.impl.FtpUtilImpl;

/**
 * 通过此类获取FTPutil
 * 
 * @author lingen.liu
 * 
 */
public class FTPFacotry {

	private static String username;

	private static String password;

	private static String serverIp;

	private static int port;
	
	private static String encoding;

	public static FtpUtil getFtpUtil(String properties) {
		initConfig(properties);
		return getFtpUtil(serverIp, port, username, password,encoding);
	}
	
	public static FtpUtil getFtpUtil(String serverIp,int port,String username,String password,String encoding) {
		FtpUtil ftpUtil = new FtpUtilImpl(serverIp, port, username, password,encoding);
		return ftpUtil;
	}

	private static void initConfig(String properties) {
		String proFilePath = null;
		try {
			proFilePath  = FTPFacotry.class.getClassLoader().getResource("").getPath() + properties;
			Properties prop = new Properties(); 
			InputStream in =new FileInputStream(new File(proFilePath));
			prop.load(in);
			username = prop.getProperty("ftp.username");
			password = prop.getProperty("ftp.password");
			serverIp = prop.getProperty("ftp.serverIp");
			port = Integer.parseInt(prop.getProperty("ftp.port"));
			encoding = prop.getProperty("ftp.encoding");
		} catch (Exception e) {
			System.out.println("===Properties Error======");
			System.out.println(proFilePath);
			e.printStackTrace();
		}
	}
	
	public static void main(String args[]){
		FtpUtil ftpUtil = FTPFacotry.getFtpUtil("ftp.properties");
		try {
		  ftpUtil.connectFtpServe();
		  
		  System.out.println(ftpUtil.isDirectory("/performance/ldwFile/A300-600"));
		  //ftpUtil.updateZipFile("/", new File("C:\\users\\lingen.liu\\Documents\\test.zip"));
		  //ftpUtil.updateDirFiles(new File("C:\\users\\lingen.liu\\Documents\\web_log"), "/", "web_log");
		 // ftpUtil.updateFile("/", new File("C:\\users\\lingen.liu\\Documents\\web_log\\"));
		 // ftpUtil.updateFile("/", new File("C:\\users\\lingen.liu\\Documents\\web_log.rar"));
		} catch (FtpException e) {
			e.printStackTrace();
		}finally{
			ftpUtil.closeFtpServe();
		}
	}
}
